package com.yrk.servlet;

import com.google.gson.Gson;
import com.yrk.dao.EmpDao;
import com.yrk.entity.Emp;
import com.yrk.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/table")
public class EmpListServlet extends HttpServlet {
    ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
//    @Autowired
//    private EmpService empService;
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        EmpDao empDao = (EmpDao) ac.getBean("empDao");
        List<Emp> list = empDao.getAllEmp();
        Gson gson = new Gson();
        String json = gson.toJson(list);
        PrintWriter out = response.getWriter();
        out.print(json);
        out.flush();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
